import { ActionTree, ActionContext } from 'vuex';
// eslint-disable-next-line import/no-cycle
import { RootState } from '@/store';
import { AppState } from './state';
import { AppActionTypes, AppMutationTypes } from './types';
import { Mutations } from './mutations';

type AugmentedActionContext = {
  commit<K extends keyof Mutations>(
    key: K,
    payload?: Parameters<Mutations[K]>[1],
  ): ReturnType<Mutations[K]>
} & Omit<ActionContext<AppState, RootState>, 'commit'>

export interface Actions {
  [AppActionTypes.ACTION_TOGGLE_SIDEBAR](
    { commit }: AugmentedActionContext,
  ): void,
  [AppActionTypes.ACTION_SET_ACTIVE_MENU](
    { commit }: AugmentedActionContext,
    activeMenu: string,
  ): void,
}

export const actions: ActionTree<AppState, RootState> & Actions = {
  [AppActionTypes.ACTION_TOGGLE_SIDEBAR]({ commit }: AugmentedActionContext) {
    commit(AppMutationTypes.TOGGLE_SIDEBAR);
  },
  [AppActionTypes.ACTION_SET_ACTIVE_MENU]({ commit }: AugmentedActionContext, activeMenu: string) {
    commit(AppMutationTypes.SET_ACTIVE_MENU, activeMenu);
  },
};
